package com.qf.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.entity.Cashier;
import com.qf.hospital.enums.HospitalErrorEnums;
import com.qf.hospital.exception.HospitalException;
import com.qf.hospital.mapper.CashierMapper;
import com.qf.hospital.service.CashierService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.service.impl
 * @Description: ${TODO}
 * @Date 2021/10/21 15:49
 */
@Service
public class CashierServiceImpl implements CashierService {

    private Logger log = LoggerFactory.getLogger(CashierServiceImpl.class);

    @Autowired
    private CashierMapper cashierMapper;

    @Override
    public PageInfo<Cashier> findMedicineByReportId(Integer perid, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Cashier> list = cashierMapper.findMedicineByReportId(perid);
        PageInfo<Cashier> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int findNotPayMedicineByName(Integer reid, String mename) {
        int state = cashierMapper.findNoGiveMedicine(reid, mename);
        if (state == 0){
            return 0;
        }
        return 1;
    }

    @Override
    public void addCashier(Cashier cashier) {
        cashier.setCtime(new Date());
        cashier.setMstate(0);
        cashier.setOstate(0);
        cashier.setState(0);
        int i = cashierMapper.insertSelective(cashier);
        if (i != 1){
            log.error("【添加出纳单信息】 添加失败 cashier = {} ",cashier);
            throw new HospitalException(HospitalErrorEnums.ROLE_ADD_ERROR);
        }
    }

    @Override
    public void del(Integer cashierId) {
        int i = cashierMapper.deleteByPrimaryKey(cashierId);
        if (i != 1){
            log.error("【删除药品信息】 删除失败 cashierId = {} ",cashierId);
            throw new HospitalException(HospitalErrorEnums.MEDICINE_DELETE_ERROR);
        }
    }

    @Override
    public PageInfo<Cashier> findXiangmuByReportId(Integer perid, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Cashier> list = cashierMapper.findXiangmuByReportId(perid);
        PageInfo<Cashier> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void delo(Integer cashierId) {
        //int i = cashierMapper.deleteByLogical(cashierId);
        int i = cashierMapper.deleteByPrimaryKey(cashierId);
        if (i != 1){
            log.error("【删除药品信息】 删除失败 cashierId = {} ",cashierId);
            throw new HospitalException(HospitalErrorEnums.MEDICINE_DELETE_ERROR);
        }
    }

    @Override
    public Integer findNotPayXiangMuByName(Integer reid, String durgname) {
        int state = cashierMapper.findNoGiveXiangMu(reid, durgname);
        if (state == 0){
            return 0;
        }
        return 1;
    }

    @Override
    public void jiaNum(Integer durgnum) {
        cashierMapper.updateCountByDrugNum(durgnum);
    }


}
